<template>
	<view class="new-users copy-data">
		<!-- 筛选条件和搜索框 -->
		<view class="searchBox">
			<view class="filterBox">
				<picker @change="onFilterChange" :value="filterIndex" :range="filterOptions" class="filterPicker">
					<view class="filterText">{{ filterOptions[filterIndex] }}</view>
				</picker>
			</view>
			<uni-search-bar ref="searchBar" radius="30" placeholder="请输入关键字" clearButton="auto" cancelButton="none"
				@confirm="onSearchConfirm" class="searchInput" />
		</view>

		<!-- 消费记录列表 -->
		<view class="moneyBox" v-for="(item, index) in userConsumeList" :key="index">
			<view class="topMoney">
				<text style="text-align: left;">{{ item.goodsName }}</text>
				<text style="text-align: right;color: #1D85E1;">{{ item.price }}</text>
			</view>
			<view class="money-details">
				<p>核销时间：{{ item.consumeTime }}</p>
				<p>订单编号：{{ item.orderNo }}</p>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getUserInfo
	} from '@/api/user.js';
	import {
		getUserConsume
	} from '@/api/business.js';

	export default {
		data() {
			return {
				userInfo: {},
				userConsumeList: [],
				pageSize: 10, // 每页显示条目数
				pageNum: 1, // 当前页码
				searchKeyword: null, // 搜索关键字
				filterIndex: 0, // 筛选条件索引，默认为 userId
				filterOptions: ['按用户ID', '按手机号'] // 筛选条件选项
			};
		},
		onLoad() {
			this.getUserDetail();
		},
		methods: {
			getUserDetail() {
				getUserInfo().then((res) => {
					this.userInfo = res.data;
					this.getUserConsumeList(this.userInfo.msqStore.id);
				});
			},
			async getUserConsumeList(storeId) {
				const data = {
					storeId: storeId,
					pageSize: this.pageSize,
					pageNum: this.pageNum,
					userId: this.filterIndex == 0 ? this.searchKeyword : null,
					userPhone: this.filterIndex == 1 ? this.searchKeyword : null,
				};

				try {
					const res = await getUserConsume(data);
					const newList = res.data.records || []; // 假设返回的数据结构有一个records字段
					// 追加到现有列表
					this.userConsumeList = this.userConsumeList.concat(newList);
					// 更新当前页码
					this.pageNum++;
				} catch (error) {
					console.error('获取消费记录失败:', error);
				}
			},
			onFilterChange(e) {
				// 保存筛选条件索引并清除搜索关键字
				this.filterIndex = e.detail.value;
				this.searchKeyword = '';
				this.$refs.searchBar.clear(); // 调用 uni-search-bar 的 clear 方法清空输入框
			},
			onSearchConfirm(e) {
				// 保存搜索关键字并重置分页
				this.searchKeyword = e.value;
				this.pageNum = 1;
				this.getUserConsumeList(this.userInfo.msqStore.id);
			}
		},
		onReachBottom() {
			// 触底加载更多
			this.getUserConsumeList(this.userInfo.msqStore.id);
		}
	};
</script>

<style lang="scss" scoped>
	page,
	body {
		height: 100%;
	}

	.searchBox {
		background-color: #fff;
		padding: 20rpx;
		display: flex;
		align-items: center;

		.filterBox {
			margin-right: 20rpx;
		}

		.filterPicker {
			width: 150rpx; // 设置筛选条件选择器的宽度
		}

		.searchInput {
			flex-grow: 1;
		}
	}

	.moneyBox {
		display: flex;
		flex-wrap: wrap;
		padding: 36rpx 43rpx;
		margin: 30rpx;
		border-radius: 20rpx;
		background: #fff;

		.topMoney {
			width: 100%;
			border-bottom: 2rpx solid #999;
			padding-bottom: 20rpx;
			font-weight: bold;
			font-size: 34rpx;
			display: flex;
			justify-content: space-between;
		}

		.money-details {
			width: 100%;
			margin-top: 20rpx;
			text-align: left;

			p {
				font-weight: 400;
				font-size: 26rpx;
				color: #999999;
			}
		}
	}
</style>